<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="添加版本"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="使用日期" prop="timeRange">
                <el-date-picker
                    v-model="formData.timeRange"
                    type="daterange"
                    start-placeholder="生效日期"
                    end-placeholder="失效日期"
                />
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="formData.remark"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定添加版本吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">立即添加</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {LineSectionVersionApi} from '@/api/intercity/LineSectionVersionApi'

export default {
    name: 'LineSectionVersionAdd',
    components: {},
    data() {
        return {
            formData: {
                sectionId: 0,
                startTime: '',
                endTime: '',
                remark: '',

                timeRange: [],
            },

            dialogVisible: false,
            rules: {
                timeRange: [
                    {required: true, message: '请选择生效时间', trigger: 'change'}
                ],
            }
        }
    },
    methods: {
        // 显示组件
        showAdd(sectionId) {
            this.formData.sectionId = sectionId
            this.dialogVisible = true
            if (this.$refs['formRef'] !== undefined) {
                this.$refs['formRef'].resetFields()
            }
        },

        // 提交表单
        submitForm() {
            const that = this
            that.formData.startTime = this.formData.timeRange[0]
            that.formData.endTime = this.formData.timeRange[1]

            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    LineSectionVersionApi.insert(that.formData).then((response) => {
                        that.dialogVisible = false
                        that.$emit('refresh')
                    })
                }
            })
        }
    }
}
</script>
